﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Collections;
using System.Xml.Linq;
using System.IO;
using OOP;
namespace DAL
{
    public class ArcustomerRepository : BaseDataClass
    {
        private const string SP_GETALL = "auto_complete_demo";
        private const string SP_GETROWS = "update_autocomplete";

        //lấy rows
        public int Rows()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_GETROWS);
            int result = (int)ExecuteScalarCmd(sqlCmd);
            return result;

        }
        //ghi vào xml   
        public void GhiVaoXMl(string rootpath)
        {

            //khai bao list auto
            XmlDocument doc = new XmlDocument();
            XmlTextWriter xtw = new XmlTextWriter(rootpath, null);
            xtw.Formatting = Formatting.Indented;
            xtw.WriteStartDocument();
            xtw.WriteStartElement("root");
            xtw.WriteElementString("Rows", Rows().ToString());
            using (SqlCommand cmd = new SqlCommand(SP_GETALL, GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                //thuc thi va lay du lieu tu database len. gan vao trong reader
                SqlDataReader reader = cmd.ExecuteReader();
                //doc tat ca du lieu ra
                while (reader.Read())
                {
                    xtw.WriteStartElement("info");
                    //ghi dong cusid 
                    xtw.WriteElementString("custid", reader["custid"].ToString());
                    //ghi dong cus name
                    xtw.WriteElementString("customer_name", reader["customer_name"].ToString());
                    //add ui nay vao trong list
                    xtw.WriteEndElement();
                }
            }
            xtw.WriteEndDocument();
            CloseConnection();
            xtw.Close();
        }
        //search trong xml
        public List<string> Search(string rootpath, string valuesearch)
        {
            //khai bao list auto
            /*thừa
            var maps = from c in XElement.Load(rootpath).Elements("info")
                       select c;
             */

            List<string> mapList = new List<string>();
            //khai báo maplist 
            XmlDocument doc = new XmlDocument();
            StreamReader sr = new StreamReader(rootpath);
            //đường dẫn đọc file xml
            doc.Load(sr);
            XmlNodeList addlst = doc.SelectNodes("root/info/customer_name");
            //đọc node theo root/info/customer_name
            foreach (XmlNode node in addlst)
            {
                if (node.InnerText.IndexOf(valuesearch, StringComparison.Ordinal) >= 0)
                {
                    mapList.Add(node.InnerText.ToString());
                }
            }
            sr.Close();
            sr.Dispose();
            return mapList;
        }
        public bool update(string rootpath)
        {
            XmlDocument doc = new XmlDocument();
            StreamReader sr = new StreamReader(rootpath);
            doc.Load(sr);
            XmlNodeList addlst = doc.SelectNodes("root/Rows");
            foreach (XmlNode node in addlst)
            {
                //sai khi
                if (node.InnerText.Equals(Rows().ToString()))
                {
                    return true;
                }
                else
                    return false;
            }
            sr.Close();
            sr.Dispose();
            return false;
        }
    }
}
